import logging

import pytest

from resource.pom.settings import Settings
from resource.pom.system_ui import SystemUI


class Test:
    system_ui = SystemUI
    app = Settings

    @pytest.mark.parametrize('setup_teardown', [app.bundle_name], indirect=True)
    def test(self, setup_teardown, device):
        logging.info('步骤1：进入桌面，右上角下划进入控制中心')
        device.start_ability(self.app.bundle_name, self.app.ability_name)
        device.refresh_layout()
        bluetooth = device.get_element_by_text('蓝牙')
        device.click_element(bluetooth)
        device.refresh_layout()
        status0 = device.get_element_by_type('Toggle')
        x = 0
        y = 0
        for i in range(2):
            logging.info('步骤2.{}：点击控制中心蓝牙按钮'.format(i))
            device.dropdown_controller_center()
            if x == 0 and y == 0:
                device.refresh_layout()
                bluetooth = device.get_element_by_text('蓝牙')
                x, y = device.center_of_element(bluetooth)
            device.wait(1)
            device.click(x - 90, y)
            device.wait(5)
            # 收起控制中心进设置看蓝牙状态
            device.swipe_up_dropdown_pan()
            device.refresh_layout()
            logging.info('预期结果2.{}：与设置蓝牙同步变化，无异常'.format(i))
            status1 = device.get_element_by_type('Toggle')
            assert status0.get('checked') != status1.get('checked'), '开关状态变化无效'
            status0 = status1
